<?php
function format_module($module)
{
    	return ucfirst(strtolower($module));
}
function format_action($action)
{
    	return ucfirst(strtolower($action))."Action";
}
function format_method($method)
{
    	return "do".ucfirst(strtolower($method));
}
function re_module($module)
{
    	return strtolower($module);
}
function re_action($action)
{
    	return str_replace("action",'',strtolower($action));
}
function re_method($method)
{
		return str_replace("do",'',strtolower($method));
}
function class_filename($class)
{
    	return $class.'.php';
}
function get_default()
{
	    $zend_config = Zend_Registry::get("zend_config");
		$default['module'] = $zend_config->application->C->module->default;
		$default['action'] = $zend_config->application->C->action->default;
		$default['method']= $zend_config->application->C->method->default;
		return array_lower($default);
}
function get_module_array()
{
	$zend_config = Zend_Registry::get("zend_config");
	$modules = $zend_config->application->C->module->names;
	$module_array = explode('|',$modules);
	return array_lower($module_array);
}
function get_module_router($module)
{
	$zend_config = Zend_Registry::get("zend_config");
	return $zend_config->application->C->router->$module;
}
function get_enter_file()
{
	$zend_config = Zend_Registry::get("zend_config");
	$enter_file = $zend_config->application->C->enterfile;
	return $enter_file;
}
function get_image_path()
{
	$zend_config = Zend_Registry::get("zend_config");
	$image_path = $zend_config->image->path;
	return $image_path;
}
function get_image_httppath()
{
	$zend_config = Zend_Registry::get("zend_config");
	$image_path = $zend_config->image->httppath;
	return $image_path;
}
function load_regex_map($name)
{
	$zend_config = Zend_Registry::get("zend_config");
	$applicationRoot = Zend_Registry::get("applicationRoot");
	$map_path = $zend_config->application->C->router->regex->map;
	$regex_map = $applicationRoot.'/'.$map_path.'/'.$name.'_map.php';
	return $regex_map;
}
function load_interceptor_map()
{
	$applicationRoot = Zend_Registry::get("applicationRoot");
	$interceptor_map_path = $applicationRoot.'/System/interceptor_map.php';
	return $interceptor_map_path;
}
function load_interceptor($name)
{
	$applicationRoot = Zend_Registry::get("applicationRoot");
	$interceptor_path = $applicationRoot.'/Interceptor/'.$name.".php";
	return $interceptor_path;
}
function load_template_path($module, $action, $method)
{
	$template_path = '/Tpl/'.$module.'/'.$action.'/'.$method.'.htm';
	return $template_path;
}
function load_action_path($module, $action)
{
	$applicationRoot = Zend_Registry::get("applicationRoot");
	$action_path = $applicationRoot.'/Application/'.$module.'/'. class_filename($action);
	return $action_path;
}
function load_helper_path($name)
{
	$applicationRoot = Zend_Registry::get("applicationRoot");
	$helper_path = $applicationRoot.'/Helper/'.class_filename($name);
	return $helper_path;
}
function array_lower($array)
{
	if(is_array($array))
	{
		foreach($array as $k =>$v)
		{
			$lower_array[$k] = strtolower($v);
		}
	}
	return $lower_array;
}
function daddslashes($string) 
{
	define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
	if(!MAGIC_QUOTES_GPC) 
	{
		if(is_array($string)) 
		{
			foreach($string as $key => $val) 
			{
				$string[$key] = daddslashes($val);
			}
		} 
		else 
		{
			$string = addslashes($string);
		}
	}
	return $string;
}
function addslashes_array($string) 
{
	
	if(is_array($string)) 
	{
		foreach($string as $key => $val) 
		{
			$string[$key] = addslashes_array($val);
		}
	} 
	else 
	{
		$string = addslashes($string);
	}
	
	return $string;
}
function dstripslashes($string) 
{
	define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
	if(MAGIC_QUOTES_GPC) 
	{
		if(is_array($string)) 
		{
			foreach($string as $key => $val) 
			{
				$string[$key] = dstripslashes($val);
			}
		} 
		else 
		{
			$string = stripslashes($string);
		}
	}
	return $string;
}
function dhtmlspecialchars($string) 
{
	
		if(is_array($string)) 
		{
			foreach($string as $key => $val) 
			{
				$string[$key] = dhtmlspecialchars($val);
			}
		} 
		else 
		{
			$string = htmlspecialchars($string, ENT_QUOTES);
		}
	
	
	return $string;
}
function dhtmlspecialchars_decode($string) 
{
	
		if(is_array($string)) 
		{
			foreach($string as $key => $val) 
			{
				$string[$key] = dhtmlspecialchars_decode($val);
			}
		} 
		else 
		{
			$string = htmlspecialchars_decode($string, ENT_QUOTES);
		}
	
	
	return $string;
}
function escape($str)
{
	preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$newstr);
	$ar = $newstr[0];
	foreach($ar as $k=>$v)
	{
		if(ord($ar[$k])>=127)
		{
			$tmpString=bin2hex(iconv("GBK","ucs-2//IGNORE",$v));
			if (!eregi("WIN",PHP_OS))
			{
				$tmpString = substr($tmpString,2,2).substr($tmpString,0,2);
			}
			$reString.="%u".$tmpString;
		}
		else
		{
			$reString.= rawurlencode($v);
		}
	}
	return $reString;
}
function unescape($str) 
{
	$str = rawurldecode($str);
	preg_match_all("/(?:%u.{4})|.+/",$str,$r);
	$ar = $r[0];
	foreach($ar as $k=>$v) 
	{
		if(substr($v,0,2) == "%u" && strlen($v) == 6) 
		{
			$ar[$k] = iconv("UCS-2","UTF-8",pack("H4",substr($v,-4)));
		}
	}
	return join("",$ar);
}
function Curl_get($url,$timeout=30, $retry = 2)
{
	$re = false;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; InfoPath.1; CIBA)");
	curl_setopt($ch, CURLOPT_ENCODING, "gzip"); 
	curl_setopt($ch, CURLOPT_REFERER, $url);
	for($i = 1; $i <= $retry; $i++)
	{
		$re = curl_exec($ch);
		if($re !== false)
		{
			curl_close($ch); break;
		}
		else 
		{
			if($i == $retry) {curl_close($ch);break;}
		}
	}
	return $re;
}
 	function regUBB($content)
	{
		$regUrlExp = '/\[url=(.*)?\](.*)?\[\/url\]/';
		
		@preg_match_all($regUrlExp, $content, $matches);
		$newcontent = $content;
		if(!empty($matches[1]))
		{
			foreach($matches[1] as $k => $v)
			{
				$replace = "<a href=\"$v\" />{$matches[2][$k]}</a>";
				$newcontent = str_replace($matches[0][$k], $replace, $newcontent);
			}
		}
		return $newcontent;
		
	}
	function regExpContent($content, $w, $h)
	{
		$regUrlExp = '/(?:https?):\/\/[-_\.a-zA-Z0-9]+[\.](?::[0-9]+)?[^ \r\n\t<>]+/';
		
		@preg_match_all ($regUrlExp, $content, $matches);
		$url_array = $matches[0];
		$newcontent = $content;
		$word   = array("\r\n", "\n", "\r"," ", "\t");
		$replace = array('<br/>','<br/>','<br/>',"&nbsp;","&nbsp;&nbsp;&nbsp;&nbsp;");
		$newcontent = str_replace($word, $replace, $newcontent);
		$temp = array();
		if(!empty($url_array))
		{
				$i=0;
				foreach($url_array as $k => $v)
				{
					if(in_array($v, $temp)) continue;
					$temp[] = $v;
					
					$ext = explode('.',basename($v));
					
					if($ext[1] == 'jpg' || $ext[1] == 'png' || $ext[1] == 'gif' || $ext[1] == 'bmp')
					{
						$replace = "<img  class=\"UIMG\" iw=\"$w\" ih=\"$h\" src=\"/pics/blank.gif\"  isrc=\"$v\" />";
						if($i==0)
						{
							$pic = $v;
							$i++;
						}
					}
					else
					{
						
						$replace = "<a href=\"$v\" />$v</a>";
						continue;
						
					}
					$newcontent = str_replace($v, $replace, $newcontent);
				}
		
		}
		return $newcontent;
	}
?>